package example.sharding.service;

import blingking.jfinal.orm.annotation.JFinalORMTx;
import blingking.jfinal.orm.tx.TxTypeEnum;
import example.sharding.model.ShardingOrder;
import example.sharding.model.Torder;
import example.sharding.model.TorderSecond;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author King
 * @description
 * @Date 2020/3/22
 */
@Service
public class OrderService {

    @JFinalORMTx(txType = TxTypeEnum.XA)
    public boolean addOrder(){
        for (int i = 0; i < 100; i++) {
            ShardingOrder shardingOrder = new ShardingOrder();
            shardingOrder.setGoodsId(i);
            shardingOrder.setAmount(new BigDecimal("222"));
            shardingOrder.setOrderNum("2222");
            //jfinal生成的sql语句包含id sharding规则如果insert语句有id则不使用自身的id生成规则
            //如果使用sharding的生成规则 则remove id
            shardingOrder.remove("id");
            shardingOrder.save();
        }

        Torder torder = new Torder();
        torder.setGoodsId(2);
        torder.setOrderNum("222222");
        torder.setAmount(new BigDecimal("222"));
        torder.save();

        TorderSecond torder2 = new TorderSecond();
        torder2.setGoodsId(2);
        torder2.setOrderNum("222222");
        torder2.setAmount(new BigDecimal("222"));
        boolean bool = torder2.save();

        return bool;
    }

}
